<?php
/**
 * Yapary_Form_Formular_Handler Meta-Generator
 *
 * @author		Joachim Ruf
 * @copyright	Copyright (c) 2003-2013 Loresoft Software (http://www.loresoft.de)
 * @license		http://yapary.loresoft.de/lizenzbedingungen     New BSD License
 *
 * */


class Yapary_Form_Formular_Meta_Generator
{
	private $oFormular		= NULL;
	private $xml			= NULL;
	private $dtd			= NULL;
	private $languageCode	= NULL;


	/**
	 * Meta-Generator fuer Yapary_Form_Formular_Handler
	 * */
	function __construct (Yapary_Form_Formular_Handler &$_oFormular, $_xml, $_languageCode) {
		$this->oFormular	= $_oFormular;
		$this->xml			= $_xml;
		$this->dtd			= NULL;
		$this->languageCode	= $_languageCode;
	}


	private function getRowList (SimpleXMLElement $_oSimpleXmlElement) {
		$count = count($_oSimpleXmlElement);

		if ($count) {
			for ($i=0; $i<$count; $i++) {
				$this->getRow($_oSimpleXmlElement[$i]);
			}
		}
	}

	private function getRow (SimpleXMLElement $_oSimpleXmlElement) {
		$desc		= (string) (isset($_oSimpleXmlElement->desc)) ? $_oSimpleXmlElement->desc : '';
		$hiddenText = (string) (isset($_oSimpleXmlElement->minimized)) ? $_oSimpleXmlElement->minimized : NULL;
		$row = $this->oFormular->addRow(utf8_decode($desc), utf8_decode($hiddenText));

		if (isset($_oSimpleXmlElement->element->component)) {
			$this->getElementList($row, $_oSimpleXmlElement->element);
		}
	}

	private function getElementList (Yapary_Form_Formular_Row $row, SimpleXMLElement $_oSimpleXmlElement) {
		$count = count($_oSimpleXmlElement);
		if ($count) {
			for ($i=0; $i<$count; $i++) {
				$this->getElement($row, $_oSimpleXmlElement[$i]);
			}
		}
	}

	private function getElement (Yapary_Form_Formular_Row $row, SimpleXMLElement $_oSimpleXmlElement) {
		$oMetaElement = new Yapary_Form_Formular_Meta_Element($_oSimpleXmlElement);
		$element = $row->addElement($oMetaElement->getComponent());

		$this->getValidation($element, $_oSimpleXmlElement);
	}


	private function getValidation (Yapary_Form_Formular_Element $_element, SimpleXMLElement $_oSimpleXmlElement) {

		$count = count($_oSimpleXmlElement->validation->object);
		if ($count) {
			for ($i=0; $i<$count; $i++) {
				$oMetaValidation = new Yapary_Form_Formular_Meta_Validation($_oSimpleXmlElement);
				$oValidation = $oMetaValidation->getValidation();

				if ($oValidation === NULL) {
					continue;
				}

				$required = (isset($_oSimpleXmlElement->component->required)) ? true : false;
				$_element->addValidation($oValidation, $required);
			}
		}
	}


	/**
	 * @desc Liefert das bis zum Punkt X generierte Formular zurueck
	 * @return Yapary_Form_Formular_Handler
	 * */
	public function &getFormular () {
		return $this->oFormular;
	}


	/**
	 * @desc Generiert aus der angegebenen XML-Definition den notwendigen PHP-Code fuer Yapary_Form_Formular_Handler
	 * @param int $_formId	: FormId der Form-Definition der XML, aus welchem das Formular generiert werden soll
	 * @return void
	 * */
	public function addFormular ($_formId) {
		$languageCode = $this->languageCode;
		$oSimpleXMlElement = new SimpleXMLElement($this->xml);

		$xPath		= "/root/language[@id=\"$languageCode\"]/form[@id=\"$_formId\"]";
		$arrResult	= $oSimpleXMlElement->xpath($xPath);

		if (count($arrResult) == 0) {
			throw new Exception('Unable to find XML form Id "'.$_formId.'"');
		}
		$oSimpleXmlElement = $arrResult[0];

		$this->getRowList($oSimpleXmlElement->row);
	}


	/**
	 * @desc Generiert aus der angegebenen XML-Definition den notwendigen PHP-Code fuer Yapary_Form_Formular_Handler
	 * @param int $_formId	: FormId der Form-Definition der XML, aus welchem das Formular generiert werden soll
	 * @return void
	 * */
	public function addElement ($_elementId) {
		$languageCode = $this->languageCode;
		$oSimpleXMlElement = new SimpleXMLElement($this->xml);

		$xPath		= "/root/language[@id=\"$languageCode\"]/form/row/element[@id=\"$_elementId\"]";
		$arrResult	= $oSimpleXMlElement->xpath($xPath);

		if (count($arrResult) == 0) {
			throw new Exception('Unable to find XML form Id "'.$_formId.'"');
		}

		$oSimpleXmlElement = $arrResult[0];
		$arrParent = $oSimpleXMlElement->xpath("/root/language[@id=\"$languageCode\"]/form/row/element[@id=\"$_elementId\"]/parent::*");
		if (count($arrParent) == 0) {
			throw new Exception('Unable to find XML form Id "'.$_formId.'"');
		}

		$oRow = $this->oFormular->addRow(utf8_decode($arrParent[0]->desc));
		$this->getElement($oRow, $oSimpleXmlElement);
	}


	public function getListNameId () {
		$arrResult = array();

		$languageCode = $this->languageCode;
		$oSimpleXMlElement = new SimpleXMLElement($this->xml);

		$xPath		= "/root/language[@id=\"$languageCode\"]/form/row/element";
		$arrResultXml	= $oSimpleXMlElement->xpath($xPath);

		if (count($arrResultXml) == 0) {
			throw new Exception('Unable to find XML form Id "'.$_formId.'"');
		}

		/* @var $object SimpleXmlElement */
		foreach ($arrResultXml as $key => $object) {
			$name = (string) $object->component->name;
			$attr = $object->attributes();
			$arrResult[$name] = (string)$attr['id'];
		}

		return $arrResult;
	}


}


?>